package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;

public class WarViewImpl extends Composite implements WarView {

	private static WarViewImplUiBinder uiBinder = GWT
			.create(WarViewImplUiBinder.class);

	interface WarViewImplUiBinder extends UiBinder<Widget, WarViewImpl> {
	}

	private Presenter presenter;

	@UiField
	HTMLPanel warWarningPanel;

	@UiField
	HTMLPanel noWarPanel;

	@UiField
	HTMLPanel warPanel;
	@UiField
	SpanElement warSpan;

	@UiField
	HTMLPanel winChancePanel;
	@UiField
	SpanElement winChanceSpan;

	@UiField
	HTMLPanel defeatedPanel;

	@UiField
	HTMLPanel victoriousPanel;

	@UiField
	HTMLPanel lossesPanel;
	@UiField
	SpanElement lostLocalWarShipsSpan;
	@UiField
	SpanElement lostOverseeWarShipsSpan;
	@UiField
	SpanElement lostLocalMerchantShipsSpan;
	@UiField
	SpanElement lostOverseeMerchantShipsSpan;
	@UiField
	SpanElement lostArmySpan;
	@UiField
	SpanElement lostAuxiliaSpan;
	@UiField
	SpanElement remainingFoodReservePercentSpan;
	@UiField
	SpanElement remainingPopulationPercentSpan;
	@UiField
	SpanElement lostShipBuildersSpan;
	@UiField
	SpanElement lostArtisansSpan;

	@UiField
	HTMLPanel tradeRateIncreasedPanel;
	@UiField
	SpanElement rateIncreaseSpan;

	@UiField
	HTMLPanel tradeRateDecreasedPanel;
	@UiField
	SpanElement rateDecreaseSpan;

	@UiField
	HTMLPanel tradeRatePanel;
	@UiField
	SpanElement rateSpan;

	@UiField
	Button continueButton;

	public WarViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;
	}

	@Override
	public void hideAll() {
		warWarningPanel.setVisible(false);
		noWarPanel.setVisible(false);
		warPanel.setVisible(false);
		winChancePanel.setVisible(false);
		defeatedPanel.setVisible(false);
		victoriousPanel.setVisible(false);
		lossesPanel.setVisible(false);
		tradeRateIncreasedPanel.setVisible(false);
		tradeRateDecreasedPanel.setVisible(false);
		tradeRatePanel.setVisible(false);
		continueButton.setVisible(false);
	}

	@Override
	public void showWarWarning() {
		warWarningPanel.setVisible(true);
	}

	@Override
	public void showNoWar() {
		noWarPanel.setVisible(true);
	}

	@Override
	public void showWar(String message) {
		warSpan.setInnerText(message);

		warPanel.setVisible(true);
	}

	@Override
	public void showContinue() {
		continueButton.setVisible(true);
	}

	@UiHandler("continueButton")
	void onContinueButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onContinue();
		}
	}

	@Override
	public void showWinChance(int chance) {
		winChanceSpan.setInnerText("" + chance);

		winChancePanel.setVisible(true);
	}

	@Override
	public void showDefeated() {
		defeatedPanel.setVisible(true);
	}

	@Override
	public void showVictorious() {
		victoriousPanel.setVisible(true);
	}

	@Override
	public void showLosses(int lostLocalWarShips, int lostOverseeWarShips,
			int lostLocalMerchantShips, int lostOverseeMerchantShips,
			int lostArmy, int lostAuxilia, int remainingFoodReservePercent,
			int remainingPopulationPercent, int lostShipBuilders,
			int lostArtisans) {
		lostLocalWarShipsSpan.setInnerText("" + lostLocalWarShips);
		lostOverseeWarShipsSpan.setInnerText("" + lostOverseeWarShips);
		lostLocalMerchantShipsSpan.setInnerText("" + lostLocalMerchantShips);
		lostOverseeMerchantShipsSpan
				.setInnerText("" + lostOverseeMerchantShips);
		lostArmySpan.setInnerText("" + lostArmy);
		lostAuxiliaSpan.setInnerText("" + lostAuxilia);
		remainingFoodReservePercentSpan.setInnerText(""
				+ (100 - remainingFoodReservePercent));
		remainingPopulationPercentSpan.setInnerText(""
				+ (100 - remainingPopulationPercent));
		lostShipBuildersSpan.setInnerText("" + lostShipBuilders);
		lostArtisansSpan.setInnerText("" + lostArtisans);

		lossesPanel.setVisible(true);
	}

	@Override
	public void showTradeRateIncreased(int tradeRateChange) {
		rateIncreaseSpan.setInnerText("" + tradeRateChange);

		tradeRateIncreasedPanel.setVisible(true);
	}

	@Override
	public void showTradeRateDecreased(int tradeRateChange) {
		rateDecreaseSpan.setInnerText("" + tradeRateChange);

		tradeRateDecreasedPanel.setVisible(true);
	}

	@Override
	public void showTradeRate(int tradeRate) {
		rateSpan.setInnerText("" + tradeRate);

		tradeRatePanel.setVisible(true);
	}
}
